package com.stedu.config;

import com.stedu.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


@EnableWebMvc
@ComponentScan(basePackages = "com.stedu", useDefaultFilters = false, includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class))
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;
    //配置视图解析器
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/WEB-INF/");
        internalResourceViewResolver.setSuffix(".jsp");
        return internalResourceViewResolver;
    }
    //配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)//添加拦截器
                .addPathPatterns("/admin/**");//设置拦截器拦截的路径

    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/admin/index").setViewName("index");
        registry.addViewController("/admin/GamingPlatform").setViewName("GamingPlatform");
        registry.addViewController("/admin/Games").setViewName("Games");
        registry.addViewController("/admin/Characters").setViewName("Characters");
        registry.addViewController("/").setViewName("login");
    }
    @Override

    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        /**
         *    配置静态资源放行，DispatcherServlet将对静态资源的请求转发到Servlet容器中默认的Servlet上，
         *    而不是使用DispatcherServlet本身来处理此类请求
         *
         *  配置静态资源放行
         */
        configurer.enable();
    }

}
